package com.asia5b.a5mall.home.activity

import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.home.R
import com.asia5b.a5mall.home.adapter.ChoiceCountryAdapter
import com.asia5b.a5mall.home.databinding.ActivityChoicecountryBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.CountrysRepository
import com.asia5b.a5mall.middleware.model.member.CountryModel
import com.asia5b.a5mall.middleware.utils.appCountrySelect
import com.asia5b.a5mall.middleware.utils.dddLog
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.resetMainActivity
import kotlinx.android.synthetic.main.activity_choicecountry.*

/**
 * Created by zhangyuncai on 2019/11/16.
 * 设置国家公馆
 */
@Route(path = ActCons.ChoiceCountryActivity)
class ChoiceCountryActivity : SimpleActivity<SimpleViewModel, ActivityChoicecountryBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivityChoicecountryBinding {
        return ActivityChoicecountryBinding.inflate(layoutInflater)
    }
    override var sIsDark = true
    private val mDatas = arrayListOf<CountryModel>()
    private val mAdapter by lazy {
        ChoiceCountryAdapter(mDatas)
    }


    override fun initView() {

        rv_choicecountry.run {
            layoutManager = GridLayoutManager(mActivity, 2)!!
            adapter = mAdapter
            val dividerItemDecorationVertical = DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL)
            dividerItemDecorationVertical.setDrawable(ContextCompat.getDrawable(mActivity, R.drawable.shape_choicecuontry_divider_h)!!)
            val dividerItemDecorationHorizontal = DividerItemDecoration(mActivity, DividerItemDecoration.HORIZONTAL)
            dividerItemDecorationHorizontal.setDrawable(ContextCompat.getDrawable(mActivity, R.drawable.shape_choicecuontry_divider_w)!!)
            addItemDecoration(dividerItemDecorationVertical)
            addItemDecoration(dividerItemDecorationHorizontal)
        }

        CountrysRepository.anyCallback<ArrayList<CountryModel>>({
            mDatas.clear()
            mDatas.addAll(it)
            mAdapter.notifyDataSetChanged()

            for (countryModel in it) {
                if (countryModel.country_id == appCountrySelect) {
                    dddLog("当前选中的国家公馆:${countryModel.country_name}")
                }
            }
        },CountrysRepository.country)

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = mAdapter.getItem(position)!!
            if (bean.country_id != appCountrySelect) {
                appCountrySelect = (bean.country_id == 0).judge(12, bean.country_id)
//                    ARouter.getInstance().build(ActCons.MainActivity)
//                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
//                            .navigation()
                resetMainActivity(mActivity)
            }
            finish()
        }
    }

    override fun initEvent() {
    }

    override fun initData() {
    }

}